import asyncio
from lib.log.index import watchdog_logger
from lib.config.index import device_config
from lib.log.index import exception_logger
from utils import device
import time

__service__ = "watch_dog"
__description__ = "watch asyncio services"

interval = 5000 #间隔ms
count = 0
reboot = device_config.get_reboot()

def init():
    watchdog_logger.clean()
    print("watch dog start")
    if reboot > 0:
        print(f"watchdog: reboot will after {reboot} seconds")

def check_reboot():
    global count
    running_time = count * (interval / 1000)

    # 最小60s
    if reboot < 60:
        return

    if running_time > reboot:
        exception_logger.log("watchdog: reboot")
        time.sleep(3)
        device.reboot()

async def run():
    global count
    while True:
        count += 1
        watchdog_logger.log(f"feed:{count}")
        check_reboot()
        await asyncio.sleep_ms(interval)
